import * as Api from "../../api/invoice";
import Notify from "@vant/weapp/notify/notify";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title_type: "company",
    visibleDialog: false,
    visibleDialog2: false,
    visibleTypeDialog: false,
    type: [
      { id: "company", name: "企业" },
      { id: "person", name: "个人/非企业单位" },
    ],
    temp: {
      title_type: "company",
    },
    invoice_id: "",
    toastImg: "../../images/toast-ok.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      invoice_id: options.invoice_id,
    });
  },
  /**
   * @description: 选择个人/企业
   * @return {*}
   */
  onChangeTitleType(e) {
    this.setData({ title_type: e.detail });
  },

  closeDialog() {
    this.setData({ visibleDialog: false });
  },
  onChangePicker(e) {
    this.setData({
      title_type: e.detail.value.id,
      visibleTypeDialog: false,
    });
  },
  onClose() {
    this.setData({ visibleTypeDialog: false });
  },
  /**
   * @description: 提交
   * @return {*}
   */
  okDialog() {
    const { invoice_id, title_type, temp } = this.data;

    Api.submitReInvoice({
      invoice_id,
      invoice_content: {
        ...temp,
        title_type,
      },
    })
      .then(() => {
        this.setData({ visibleDialog: false, visibleDialog2: true });
      })
      .catch(() => {
        this.setData({ visibleDialog: false });
      });
  },
  /**
   * @description: 提交-唤起二次确认
   * @param {*} e
   * @return {*}
   */
  formSubmit(e) {
    const { title, tax_no } = e.detail.value;
    if (!title) {
      Notify({
        message: "请输入发票抬头",
        top: 100,
      });
      return;
    }

    if (this.data.title_type === "company" && !tax_no) {
      Notify({
        message: "请输入税号",
        top: 100,
      });
      return;
    }
    this.setData({ temp: e.detail.value, visibleDialog: true });
  },
  /**
   * @description:
   * @return {*}
   */
  handleConfirm() {
    this.setData({ visibleDialog2: false });
    wx.redirectTo({
      url: `/pages/invoiceList/invoiceList`,
    });
  },
});
